package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.mapper.UserDDeptMapper;
import com.caifu.pojo.CDDept;
import com.caifu.pojo.SysUser;
import com.caifu.service.UserCDDeptService;
import com.caifu.util.Utils;
import com.caifu.vo.ParamsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author tww
 * ClassName: UserCDDeptServiceImpl.java、
 * createTime: 2020年12月18日 16:18:37
 **/

@Service
public class UserCDDeptServiceImpl implements UserCDDeptService {

    @Autowired
    private UserDDeptMapper userDDeptMapper;

    @Override
    public Result updateDept(ParamsVo paramsVo, SysUser user) {
        String str = paramsVo.getStr();
        //删除之前关联的部门
        UpdateWrapper<CDDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_id", paramsVo.getId()).eq("status", 1);

        List<CDDept> cdDepts = userDDeptMapper.selectList(new QueryWrapper<CDDept>()
                .eq("device_id", paramsVo.getId())
                .eq("status", 1));
        for (CDDept cdDept : cdDepts) {
            cdDept.setStatus("9");
            userDDeptMapper.updateById(cdDept);
        }
        //str不为空，添加设备关联部门
        if (Utils.isNotNull(str)) {
            String[] split = str.split(",");
            CDDept dept = new CDDept();

            dept.setDeviceId(Integer.parseInt(paramsVo.getId()));
            dept.setCreateTime(new Date());
            dept.setStatus("1");

            for (String deptId : split) {
                dept.setDeptId(Integer.parseInt(deptId));
                userDDeptMapper.insert(dept);
            }

        }

        return ResultUtil.success(200, "提交成功");
    }
}
